package com.xinfu.newproject.model.certifition;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.certifition.QeuryRoomBean;
import com.xinfu.newproject.bean.certifition.QeuryRoomBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class QeuryRoomModel implements IModel.IQeuryRoomModel {

    @Override
    public void qeuryRoom(QeuryRoomBody qeuryRoomBody, MyCallBack<QeuryRoomBean> callBack) {
        Map<String, String> map = new HashMap<>();
        map.put("projectId", qeuryRoomBody.getProjectId());
        JsNetworkService.getInstance().getIJsNetworkService()
                .queryRoomData(qeuryRoomBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<QeuryRoomBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull QeuryRoomBean qeuryRoomBean) {

                        callBack.onSuccess(qeuryRoomBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
